package com.xiaobai.geogposition;

import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

/**
 * @Author 王天文
 * @Date 2024/12/23 9:57
 * @Description: ip地址工具类
 */
@Slf4j
public class IPUtils {

    public static List<String> getCurrentIpList() {

        List<String> ipList = new ArrayList<>();
        try {
            Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
            while (networkInterfaces.hasMoreElements()) {
                NetworkInterface networkInterface = networkInterfaces.nextElement();
                Enumeration<InetAddress> inetAddresses = networkInterface.getInetAddresses();
                while (inetAddresses.hasMoreElements()) {
                    InetAddress inetAddress = inetAddresses.nextElement();

                    String hostAddress = inetAddress.getHostAddress();
                    if (!inetAddress.isLoopbackAddress() && hostAddress.indexOf(":") == -1) {
                        ipList.add(hostAddress);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return ipList;
    }
}
